package com.wootion.cims.module.systemsetting.dictionary.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wootion.cims.dao.SysDictionaryDao;
import com.wootion.cims.module.systemsetting.dictionary.model.SysDictionary;
import com.wootion.cims.module.systemsetting.dictionary.service.SysDictionaryService;
import com.wootion.cims.response.ResponseResult;
import com.wootion.cims.response.ResultCode;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryDao, SysDictionary> implements SysDictionaryService {

    @Autowired(required = false)
    SysDictionaryDao sysDictionaryDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult insert(SysDictionary sysDictionary) {
        UUID uuid= UUID.randomUUID();
        sysDictionary.setId(uuid.toString()).setEnabled(true).setCreateTime(new Date());
        int insert = sysDictionaryDao.insert(sysDictionary);
        if (insert>0){
            return ResponseResult.builder().code(ResultCode.NORMAL.getCode()).message(ResultCode.NORMAL.getMessage()).build();
        }else {
            return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message(ResultCode.SYSTEM_ERROR.getMessage()).build();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult update(SysDictionary sysDictionary) {
        int update = sysDictionaryDao.updateById(sysDictionary);
        if (update>0){
            return ResponseResult.builder().code(ResultCode.NORMAL.getCode()).message(ResultCode.NORMAL.getMessage()).build();
        }else {
            return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message(ResultCode.SYSTEM_ERROR.getMessage()).build();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteById(String parseObject) {
        SysDictionary sysDictionary = sysDictionaryDao.selectById(parseObject);
        int i = sysDictionaryDao.updateById(sysDictionary.setEnabled(false));

        if (i>0){
            return ResponseResult.builder().code(ResultCode.NORMAL.getCode()).message(ResultCode.NORMAL.getMessage()).build();
        }else {
            return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message(ResultCode.SYSTEM_ERROR.getMessage()).build();
        }
    }

    @Override
    public ResponseResult selectPage(Integer limit, Integer page,String condition,String type) {
        Page<SysDictionary> page1 = new Page<>(page,limit);

        QueryWrapper<SysDictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enabled",true);
        queryWrapper.like("name",condition);

        if (!StringUtil.isNullOrEmpty(type)){
            queryWrapper.eq("type",type);
        }

        Page<SysDictionary> userPage = sysDictionaryDao.selectPage(page1, queryWrapper);
        return ResponseResult.builder().code(ResultCode.NORMAL.getCode()).message(ResultCode.NORMAL.getMessage()).data(userPage).build();
    }
}
